package org.columba.mail.gui.table.command;

import java.util.List;

import org.columba.api.command.ICommandReference;
import org.columba.api.command.IWorkerStatusController;
import org.columba.api.gui.frame.IFrameMediator;
import org.columba.api.selection.ISelectionListener;
import org.columba.api.selection.SelectionChangedEvent;
import org.columba.core.command.Command;
import org.columba.core.command.StatusObservableImpl;
import org.columba.mail.command.IMailFolderCommandReference;
import org.columba.mail.folder.IMailFolder;
import org.columba.mail.folder.IMailbox;
import org.columba.mail.folder.virtual.VirtualFolder;
import org.columba.mail.gui.frame.IResourceTableOwner;
import org.columba.mail.gui.frame.MailFrameMediator;
import org.columba.mail.gui.frame.TableViewOwner;
import org.columba.mail.gui.socialnet.selection.VertexSelectionChangedEvent;
import org.columba.mail.gui.table.ITableController;
import org.columba.mail.gui.table.TableController;
import org.columba.mail.gui.table.action.ClearHeaderlistAction;
import org.columba.mail.gui.tree.selection.TreeSelectionChangedEvent;
import org.columba.mail.message.IHeaderList;

import com.deruan.hotcore.base.common.bean.ResourceBean;
import com.deruan.hotcore.resource.table.IResourceTableController;

public class ViewResourceHeaderListCommand extends Command implements
ISelectionListener
{
	private List<ResourceBean> headerList;

	private IMailFolder folder;

	private boolean updateGui;

	private IFrameMediator mediator;
	
	public ViewResourceHeaderListCommand(IFrameMediator mediator,
			ICommandReference reference) {
		super(reference);

		this.mediator = mediator;

		// Register as listener to the SelectionManger
		// to check for selection changes
		updateGui = true;

		((MailFrameMediator) mediator).registerSocialNetSelectionListener(this);

		priority = Command.REALTIME_PRIORITY;
	}

	/**
	 * @see org.columba.api.command.Command#updateGUI()
	 */
	public void updateGUI() throws Exception {
		((MailFrameMediator) mediator).removeSocialNetSelectionListener(this);

		// Update only if the selection did not change
		if (updateGui) {
			IResourceTableController resourcetableController = ((IResourceTableOwner) mediator).getResourceTableController();
			//resourcetableController.addColumn(resourcetableController.createTableColumn("11111", 50));
			resourcetableController.showResourceList((IMailbox)folder, headerList);
		}

	}

	/**
	 * @see org.columba.api.command.Command#execute(Worker)
	 */
	public void execute(IWorkerStatusController worker) throws Exception 
	{
		// Register as SelectionListener to track the selection
		// of the tree
		if (!updateGui)
			return;
        if(folder!=null)
		// register for status events
		((StatusObservableImpl) (((IMailbox)folder).getObservable())).setWorker(worker);

		// fetch the headerlist
		try {
			//headerList = ((IMailbox)folder).getHeaderList();
			if(folder==null)
			{
				headerList = null;
			}
			else {
				headerList = ((VirtualFolder)folder).getResourceList();
			}
			
			
		} catch (Exception e) {
			updateGui = false;

			// Reset the selection
			((MailFrameMediator) mediator).setTreeSelection(null);
			new ClearHeaderlistAction(mediator).actionPerformed(null);

			throw e;
		}

		updateGui &= !worker.cancelled();
	}

	/**
	 * @see org.columba.api.selection.ISelectionListener#selectionChanged(org.columba.api.selection.SelectionChangedEvent)
	 */
	public void selectionChanged(SelectionChangedEvent e) {

		// old command-specific selection
		IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();

		// new selection
		folder = ((VertexSelectionChangedEvent) e).getSelectedFolder();
		// abort if nothing selected
		if (folder==null)
			return;

		// cancel command execution/updateGUI methods, if folder selection
		// has been modified
		if (r.getSourceFolder().getId() != folder.getId())
			updateGui = false;
	}

}
